<?php
/**
安装bootstrap
 */
function Bootstrap() {  
    wp_register_script( 'default', get_template_directory_uri() . '/inc/bootstrap.bundle.min.js' );  
    wp_register_style( 'default', get_template_directory_uri() . '/inc/bootstrap.min.css' ); 
    if ( !is_admin() ) {
        wp_enqueue_script( 'default' );  
        wp_enqueue_style( 'default' );  
    } 
}  
add_action( 'init', 'Bootstrap' ); 


function wbstrap_scripts() {
    wp_enqueue_style( 'WB-strap', get_stylesheet_uri() );
}
add_action( 'wp_enqueue_scripts', 'wbstrap_scripts' );


/*
*/
if ( ! function_exists( 'setup' ) ) :
	function setup() {

        require_once get_template_directory() . '/functions/clean.php';
		require_once get_template_directory() . '/functions/wp-bootstrap-navwalker.php';
        require_once get_template_directory() . '/functions/wp-bootstrap-comment-walker.php';
        require_once get_template_directory() . '/functions/wp-bootstrap-pagination-walker.php';
        require_once get_template_directory() . '/admin/options.php';
        /*
        add_theme_support( 'editor-styles' );
        add_editor_style('inc/bootstrap.min.css');*/
        add_theme_support( 'wp-block-styles' );
        add_theme_support( 'title-tag' );
        add_theme_support('post-thumbnails'); 
        add_image_size('thumbnail', 400, 225, true);
        add_image_size('show', 800, 450, true);
        add_filter( 'pre_option_link_manager_enabled', '__return_true' );
        register_nav_menus( array(
         'primary' => __( 'Primary Menu', 'THEMENAME' ),
     ) );
        if (is_admin()) {
            
            function theme_settings_admin() {
                require get_template_directory()."/admin/admin.php";
            }   
        }

    }

endif;
add_action( 'after_setup_theme', 'setup' );


/**
符合bootstrap的导航输出结构,显示下拉菜单
 */
add_filter( 'nav_menu_link_attributes', 'prefix_bs5_dropdown_data_attribute', 20, 3 );

function prefix_bs5_dropdown_data_attribute( $atts, $item, $args ) {
    if ( is_a( $args->walker, 'WP_Bootstrap_Navwalker' ) ) {
        if ( array_key_exists( 'data-toggle', $atts ) ) {
            unset( $atts['data-toggle'] );
            $atts['data-bs-toggle'] = 'dropdown';
        }
    }
    return $atts;
}


/*
 分页导航
 */
 function wp_boostrap_pagination_posts_link_attributes()
 {
    return 'class="page-link"';
}
add_filter('next_posts_link_attributes', 'wp_boostrap_pagination_posts_link_attributes');
add_filter('previous_posts_link_attributes', 'wp_boostrap_pagination_posts_link_attributes');



/* 为文章添加最后更新时间 */

function wpb_last_updated_date( $content ) {
    $u_time = get_the_time('U'); 
    $u_modified_time = get_the_modified_time('U'); 
    $custom_content = ''; 
    if ($u_modified_time >= $u_time + 86400) { 
        $updated_date = get_the_modified_time('Y-F-jS');
        $updated_time = get_the_modified_time('h:i a'); 
        $custom_content .= $content.'<div class="alert alert-success" role="alert">最后更新于 '. $updated_date . ' at '. $updated_time .'</div>'; 
    } else{
        $custom_content .= $content;
    }

    return $custom_content;
}
add_filter( 'the_content', 'wpb_last_updated_date' );

/* 时间变为几天前 */
function Bing_filter_time(){
    global $post ;
    $to = time();
    $from = get_the_time('U') ;
    $diff = (int) abs($to - $from);
    if ($diff <= 3600) {
        $mins = round($diff / 60);
        if ($mins <= 1) {
            $mins = 1;
        }
        $time = sprintf(_n('%s 分钟', '%s 分钟', $mins), $mins) . __( '前' ,);
    }
    else if (($diff <= 86400) && ($diff > 3600)) {
        $hours = round($diff / 3600);
        if ($hours <= 1) {
            $hours = 1;
        }
        $time = sprintf(_n('%s 小时', '%s 小时', $hours), $hours) . __( '前' );
    }
    elseif ($diff >= 86400) {
        $days = round($diff / 86400);
        if ($days <= 1) {
            $days = 1;
            $time = sprintf(_n('%s 天', '%s 天', $days), $days) . __( '前');
        }
        elseif( $days > 29){
            $time = get_the_time(get_option('date_format'));
        }
        else{
            $time = sprintf(_n('%s 天', '%s 天', $days), $days) . __( '前');
        }
    }
    return $time;
}
add_filter('the_time','Bing_filter_time');

/* 主循环中排除置顶文章 */
function exclude_sticky_posts($query){
    if ( is_admin() || ! $query->is_main_query() )
        return;
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'ignore_sticky_posts', 1 );
    }
}  
add_action('pre_get_posts','exclude_sticky_posts');


/* 设置摘要字数和更多 */
function new_excerpt_length($length) {
    return 150;
}
add_filter('excerpt_length', 'new_excerpt_length');

function wpdocs_excerpt_more( $more ) {
    return '<a href="'.get_the_permalink().'" rel="nofollow">Read More...</a>';
}
add_filter( 'excerpt_more', 'wpdocs_excerpt_more' );



/*
 阅读次数统计
 */
 function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0";
    }
    return $count;
}
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

/* 获取文章的评论人数 by zwwooooo | zww.me */
function zfunc_comments_users($postid=0,$which=0) {
    $comments = get_comments('status=approve&type=comment&post_id='.$postid); //获取文章的所有评论
    if ($comments) {
        $i=0; $j=0; $commentusers=array();
        foreach ($comments as $comment) {
            ++$i;
            if ($i==1) { $commentusers[] = $comment->comment_author_email; ++$j; }
            if ( !in_array($comment->comment_author_email, $commentusers) ) {
                $commentusers[] = $comment->comment_author_email;
                ++$j;
            }
        }
        $output = array($j,$i);
        $which = ($which == 0) ? 0 : 1;
        return $output[$which]; //返回评论人数
    }
    return 0; //没有评论返回0
}



/* 缩略图 */
function post_thumbnail_src($size){
    global $post;
    if( $values = get_post_custom_values("show") ) {
$values = get_post_custom_values("show"); //在文章中的自定义字段中show对应的图片地址
$post_thumbnail_src = $values [0];
} elseif( has_post_thumbnail() ){
    switch($size){
        case 'thumbnail':
        $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail');
        $post_thumbnail_src = $thumbnail_src [0];
        break;
        case 'show':
        $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'show');
        $post_thumbnail_src = $thumbnail_src [0];
        break;
        default:
        break;
    }
} else {
$post_thumbnail_src = '';
};
echo $post_thumbnail_src;
}






add_action('admin_menu', 'add_theme_options_menu');


function add_theme_options_menu() {
    add_theme_page(
            '主题设置', //页面title
            '主题设置', //后台菜单中显示的文字
            'edit_theme_options', //选项放置的位置
            'theme-options', //别名，也就是在URL中GET传送的参数
            'theme_settings_admin' //调用显示内容调用的函数
        );
}








